package com.luntan.activity;

import java.util.ArrayList;
import java.util.Random;

import com.luntan.adapter.ListViewAdapters;
import com.luntan.bean.Forumbean;
import com.luntan.utils.Utils;
import com.luntan.view.R;
import com.luntan.widget.AutoListView;
import com.luntan.widget.AutoListView.OnLoadListener;
import com.luntan.widget.AutoListView.OnRefreshListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class GoForumActivity extends Activity implements OnRefreshListener,
		OnLoadListener,OnItemClickListener,OnItemLongClickListener {

	private AutoListView lstv;
	private ListViewAdapters adapter;
	private ArrayList<Forumbean> list = new ArrayList<Forumbean>();
//	List<String> list=new ArrayList<String>();
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
	  	    ArrayList<Forumbean> result = (ArrayList<Forumbean>) msg.obj;
//			List<String> result = (List<String>) msg.obj;
			switch (msg.what) {
			case AutoListView.REFRESH:
				lstv.onRefreshComplete();
				list.clear();
				list.addAll(result);
				break;
			case AutoListView.LOAD:
				lstv.onLoadComplete();
				list.addAll(result);
				break;
			}
			lstv.setResultSize(result.size());
			adapter.notifyDataSetChanged();
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		lstv = (AutoListView) findViewById(R.id.lstv);
		adapter = new ListViewAdapters(this, list);
		lstv.setAdapter(adapter);
		lstv.setOnRefreshListener(this);
		lstv.setOnLoadListener(this);
		lstv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(GoForumActivity.this,GoforumItemActivity.class);	
				startActivity(intent);
			}
		});
		initData();
	}

	private void initData() {
		loadData(AutoListView.REFRESH);
	}

	private void loadData(final int what) {
		// 这里模拟从服务器获取数据
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(700);
					//网络获取数据
//					String jsonStr = Utils.getHttpJSON("pumpStrUrl");
//					list=Utils.getList(jsonStr);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = handler.obtainMessage();
				msg.what = what;
				msg.obj = getData();
				handler.sendMessage(msg);
			}
		}).start();
	}

	@Override
	public void onRefresh() {
		loadData(AutoListView.REFRESH);
	}

	@Override
	public void onLoad() {
		loadData(AutoListView.LOAD); 
	}

	// 测试数据
	public ArrayList<Forumbean> getData() {
		ArrayList<Forumbean> result=new ArrayList<Forumbean>();
		Random random = new Random();
		for (int i = 0; i < 10; i++) {
			long l = random.nextInt(10000);
			Forumbean forum=new Forumbean();
			forum.setName("你好");
			forum.setTime(Utils.getCurrentTime());
			forum.setContent("我在这里呀，我在这里呀");
			forum.setImageUrl("l");
			forum.setContentImageUrl("l");
			result.add(forum);
//			result.add("当前条目的ID：" + l);
		}
		return result;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Log.e("hha", "hahaaaaa");
		Intent intent=new Intent(GoForumActivity.this,GoforumItemActivity.class);	
		startActivity(intent);
		
	}


	
	
}
